<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<div id="shared_fileToolbar">
			<div class="shared_fileToolbar_button" style="height: 40px;">
				<a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="shared_file_upload()" plain="true">上传</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-save" onclick="shared_file_download()" plain="true">下载</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="shared_file_stateEdit()" plain="true">分享</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="shared_file_remove()" plain="true">删除</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="shared_file_cancel()" plain="true">取消</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-reload" onclick="shared_file_reload()" plain="true">刷新</a>
			</div>
			<div style="height: 40px;">
				<label>文件名：</label><input class="easyui-textbox" id="file_searchbox" style="width:150px,height:24px;">
				<a href="#" class="easyui-linkbutton" iconCls="icon-search" id="file_searchbtn">开始检索</a>
			</div>
		</div>

		<div id="shared_file_dialog">

			<table>
				<!-- 					<tr>
						<td><input type="hidden" id="provider" name="provider"/></td>
					<tr> -->
				<tr>
					<td>
						<form id="shared_file_dialog_form" enctype="multipart/form-data">
							<input id="file" name="file" style="width:270px" type="file">
						</form>
					</td>
				</tr>
				<tr>
					<td style="width:280px;" align="left">文件说明：</td>
				</tr>
				<tr>
					<td><textarea id="notes" name="notes" rows="11" cols="35">
						</textarea></td>
				</tr>
				<tr>
					<td align="center">
						<select id="public">
							<option value="0">保密</option>
							<option value="1">公开</option>
						</select>
					</td>
				</tr>
			</table>

		</div>
		<table id="shared_file_dg"></table>

		<script type="text/javascript">
			$(function() {
				setPublic();
				$('#shared_file_dg').datagrid({
					url: pageContext_request_contextPath + 'files_page',
					method: "get",
					pagination: true,
					fitColumns:true,
					border:false,
					pageSize: 10,
					columns: [
						[{
								field: "ck",
								checkbox: true
							},
							{
								field: 'id',
								title: 'fileId',
								width: 100
							},
							{
								field: 'fileName',
								title: 'fileName',
								width: 100
							},
							{
								field: 'fileType',
								title: 'fileType',
								width: 100
							},
							{
								field: 'fileSize',
								title: 'fileSize',
								width: 100
							},
							{
								field: 'savePath',
								title: 'savePath',
								width: 100
							},
							{
								field: 'saveName',
								title: 'saveName',
								width: 100
							},
							{
								field: 'notes',
								title: 'notes',
								width: 100
							},
							{
								field: 'provider',
								title: 'provider',
								width: 100,
								formatter: function(value) {
									return value.username;
								}
							},
							{
								field: 'state',
								title: 'public',
								width: 100,
								formatter: function(value) {
									if (value == 1) {
										return '公开';
									} else {
										return '保密';
									}
								}
							},
						]
					]
				});

				$("#public option[value='1']").attr("selected", "selected");

				$('#shared_file_dialog').dialog({
					width: 300,
					height: 350,
					closed: true,
					cache: false,
					modal: false,
					iconCls: 'icon-save'
				});
				
				$("#file_searchbtn").bind("click",function(){
					var queryParams = $("#shared_file_dg").datagrid("options").queryParams;
					queryParams.filename = $("#file_searchbox").textbox("getValue");
					$("#shared_file_dg").datagrid("load");
				});

			})
			
			function shared_file_upload() {
				$("#shared_file_dialog_form").form("clear");
				$("#notes").val("");
				$("#shared_file_dialog").dialog({
					title: "上传文件",
					closed: false,
					modal: true,
					onOpen: function() {
						$("#provider").val($(".current-id").text());
					},
					buttons: [{
							text: "确定",
							iconCls: "icon-ok",
							handler: shared_file_add
						},
						{
							text: "取消",
							iconCls: "icon-cancel",
							handler: function() {
								$("#shared_file_dialog").dialog("close");
							}
						}
					]
				});
			}

			function shared_file_stateEdit() {
				var rows = $("#shared_file_dg").datagrid("getSelections");

				if (rows.length == 0) {
					$.messager.alert("信息提示", "请勾选您要修改的数据！", "info");
				} else {
					$.messager.confirm("信息提示", "确定要修改选中的文件吗？", function(result) {
						if (result) {
							var ids = [];
							$(rows).each(function() {
								ids.push(this.id);
							});
							$.ajax({
								url: "/filesharingsystem/files",
								method: "put",
								contentType: "application/json;charset=utf-8",
								data: JSON.stringify(ids),
								success: function(data) { //"ok","error"
									if (data == "ok") {
										shared_file_reload();
									} else {
										$.messager.alert("信息提示", "修改失败！", "info");
									}
								}
							});
						}
					});

				}
			}

			function shared_file_download() {
				var rows = $("#shared_file_dg").datagrid("getSelections");
				if (rows.length < 1) {
					$.messager.alert("信息提示", "请勾选您要下载的文件！", "info");
				} else {
					$.messager.confirm("信息提示", "确定要下载选中的文件吗？", function(result) {
						if (result) {
							var ids = [];
							var fileNames = [];
							$(rows).each(function() {
								ids.push(this.id);
								fileNames.push(this.fileName);
							});

							for (var i = 0; i < ids.length; i++) {
								
								download(fileNames[i],ids[i]);
								
								
								// download("/filesharingsystem/file/"+ids[i],fileNames[i]);
								// $.ajax({
								// 	url: "/filesharingsystem/files",
								// 	method: "get",
								// 	contentType: "application/json;charset=utf-8",
								// 	data: {id: ids[i]},
								// 	success: function(data) { //"ok","error"       
								// 		var a = document.createElement("a");
								// 		a.download = fileNames[i];
								// 		a.href = window.URL.createObjectURL(data);
								// 		a.click();									
								// 	},
								// 	error: function(XMLHttpRequest, textStatus, errorThrown) {
								// 		$.messager.alert("信息提示", "下载失败！");
								// 	}
								// });
								
								
								
								
								
							}
						}
					});
				}
			}

			function shared_file_remove() {
				var rows = $("#shared_file_dg").datagrid("getSelections");
				if (rows.length < 1) {
					$.messager.alert("信息提示", "请勾选您要删除的数据！", "info");
				} else {
					$.messager.confirm("信息提示", "确定要删除选中的记录吗？", function(result) {
						if (result) {
							var ids = [];
							$(rows).each(function() {
								ids.push(this.id);
							});
							$.ajax({
								url: "/filesharingsystem/files",
								method: "delete",
								contentType: "application/json;charset=utf-8",
								data: JSON.stringify(ids),
								success: function(data) { //"ok","error"
									if (data == "ok") {
									shared_file_reload();
									} else {
										$.messager.alert("信息提示", "删除失败！", "info");
									}
								}
							});
						}
					});
				}
			}

			function shared_file_add() {

				var formdata = new FormData($("#shared_file_dialog_form")[0]);
				formdata.append('notes', $("#notes").val());
				formdata.append('state', $("#public option:selected").val());
				$.ajax({
					url: "/filesharingsystem/file",
					method:'post',
					data: formdata,
					cache: false,
					contentType: false,
					processData: false,
					success: function(data) { //"ok","error"
						$("#shared_file_dialog").dialog("close");
						shared_file_reload();	
					},
					error:function(){
						$.messager.alert("信息提示", "提交失败！", "info");
					}
				});
			}

			function shared_file_cancel() {
				$("#shared_file_dg").datagrid("rejectChanges");
			}

			function shared_file_reload() {
				$("#shared_file_dg").datagrid("reload");
			}
			
			function download(fileName,id){
				var a = document.createElement("a");
				a.download = fileName;
				a.href = "/filesharingsystem/files?id="+id;
				a.click();	
			}
			// function download(url,filename) {
			// 	var xhr = new XMLHttpRequest();
			// 	xhr.open('GET', url, true); // 也可以使用POST方式，根据接口
			// 	xhr.responseType = "blob"; // 返回类型blob
			// 	// 定义请求完成的处理函数，请求前也可以增加加载框/禁用下载按钮逻辑
			// 	xhr.onload = function() {
			// 		// 请求完成
			// 		if (this.status === 201) {
			// 			// 返回200
			// 			var blob = this.response;
			// 			var reader = new FileReader();
			// 			reader.readAsDataURL(blob); // 转换为base64，可以直接放入a表情href
			// 			reader.onload = function(e) {
			// 				// 转换完成，创建一个a标签用于下载
			// 				var a = document.createElement('a');
			// 				a.download = filename;
			// 				a.href = e.target.result;
			// 				$("body").append(a); // 修复firefox中无法触发click
			// 				a.click();
			// 				$(a).remove();
			// 			}
			// 		}
			// 	};
			// 	// 发送ajax请求
			// 	xhr.send();
			// }
			function setPublic(){
				var str = $(".level").text();
				if(str=="guest"){
					$("#public").hide();
				}
			}
		</script>
	</body>
</html>
